/*
 * 文 件    名:  ContentProviderFactory.java
 * 创建日期:  2012-10-9下午5:47:30
 * 版          权:  Epro Technologies Co., Ltd. Copyright 2011-2012,  All rights reserved
 * 作          者:  dc051
 */
package com.weipu.common.facade.content;

import java.util.HashMap;

import android.content.UriMatcher;
import android.net.Uri;

import com.weipu.common.facade.content.impl.BrowsingHistoryProviderImpl;
import com.weipu.common.facade.content.impl.CategoryContentProviderImpl;
import com.weipu.common.facade.content.impl.HomeDataContentProviderImpl;
import com.weipu.common.facade.content.impl.HotWordProviderImpl;
import com.weipu.common.facade.content.impl.MyCartProviderImpl;
import com.weipu.common.facade.content.impl.OnekeyFilterProviderImpl;
import com.weipu.common.facade.content.impl.ProductContentProviderImpl;
import com.weipu.common.facade.content.impl.SearchHistoryProviderImpl;

/**
 * 内容提供者管理类
 * 
 * @version [版本号, 2012-10-9]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 * 
 * @author dc051
 */
public class ContentProviderFactory
{
    private ContentProviderFactory()
    {
    }
    
    private static HashMap<Integer, BaseContentProvider> map;
    
    private static UriMatcher MATCHER;
    
    // 匹配根路径
    private static final int MATCHER_ROOT = 0;
    
    // // SPECIFICATION
    // private static final int MATCHER_SPECIFICATION_ALL = 3;
    //
    // // SPECIFICATION by id
    // private static final int MATCHER_SPECIFICATION_BY_ID = 4;
    
    // 购物车
    private static final int MATCHER_MYCART_ALL = 11;
    
    // 购物车 by id
    private static final int MATCHER_MYCART_BY_ID = 12;
    
    // 搜素记录
    private static final int MATCH_SEARCHHISTORY_ALL = 13;
    
    // 搜索 BY id
    private static final int MATCH_SEARCHHISTORY_BY_ID = 14;
    
    // 历史浏览
    private static final int MATCH_BRPWSINGHISTORY_ALL = 15;
    
    // 历史 by id
    private static final int MATCH_BROWSINGHISTORY_BY_ID = 16;
    
    //栏目 --begin
    // 栏目数据
    private static final int MATCH_CATEGORY_ALL = 17;
    
    // 栏目数据 by id
    private static final int MATCH_CATEGORY_BY_ID = 18;
    
    //栏目 --end
    
    //产品 --begin
    // 产品数据
    private static final int MATCH_Product_ALL = 19;
    
    // 产品数据 by id
    private static final int MATCH_Product_BY_ID = 20;
    
    //产品 --end
    
    //广告 --begin
    // 广告数据
    private static final int MATCH_Banner_ALL = 21;
    
    // 广告数据 by id
    private static final int MATCH_Banner_BY_ID = 22;
    //广告--end
    
    // 一键过滤--begin
    // 一键过滤
    private static final int MATCH_OnekeyFilter_ALL = 23;
    
    // 一键过滤 by id
    private static final int MATCH_OnekeyFilter_BY_ID = 24;
    //一键过滤--end
    
    // 首页 --begin
    // 首页 
    private static final int MATCH_Home_Module_Data_ALL = 25;
    
    // 首页  by id
    private static final int MATCH_Home_Module_Data_BY_ID = 26;
    //首页 --end
    
 // 热搜--begin
    // 热搜
    private static final int MATCH_HOT_WORD_ALL = 27;
    
    // 热搜  by id
    private static final int MATCH_HOT_WORD_BY_ID = 28;
    //热搜 --end

    
    // // RAW SQL
    // private static final int MATCHRAW = 99;
    
    static
    {
        map = new HashMap<Integer, BaseContentProvider>();
        MATCHER = new UriMatcher(MATCHER_ROOT);
        
        BaseContentProvider mycart = new MyCartProviderImpl();
        map.put(MATCHER_MYCART_ALL, mycart);
        map.put(MATCHER_MYCART_BY_ID, mycart);
        
        MATCHER.addURI(ContentURI.BASE,
            ContentURI.ContentMyCart.CONTENT_URI_ALL.getPath().replaceFirst("/", ""),
            MATCHER_MYCART_ALL);
        MATCHER.addURI(ContentURI.BASE, ContentURI.ContentMyCart.CONTENT_URI_BY_ID.getPath().replaceFirst("/", "")
            + "/#", MATCHER_MYCART_BY_ID);
        // 搜索记录
        BaseContentProvider searchhistorys = new SearchHistoryProviderImpl();
        map.put(MATCH_SEARCHHISTORY_ALL, searchhistorys);
        map.put(MATCH_SEARCHHISTORY_BY_ID, searchhistorys);
        MATCHER.addURI(ContentURI.BASE,
            ContentURI.ContentSearchHistory.CONTENT_URI_ALL.getPath().replaceFirst("/", ""),
            MATCH_SEARCHHISTORY_ALL);
        MATCHER.addURI(ContentURI.BASE,
            ContentURI.ContentSearchHistory.CONTENT_URI_BY_ID.getPath().replaceFirst("/", "") + "/#",
            MATCH_SEARCHHISTORY_BY_ID);
        // 历史记录
        BaseContentProvider browsingHistory = new BrowsingHistoryProviderImpl();
        map.put(MATCH_BRPWSINGHISTORY_ALL, browsingHistory);
        map.put(MATCH_BROWSINGHISTORY_BY_ID, browsingHistory);
        
        MATCHER.addURI(ContentURI.BASE,
            ContentURI.ContentBrowsingHistory.CONTENT_URI_ALL.getPath().replaceFirst("/", ""),
            MATCH_BRPWSINGHISTORY_ALL);
        
        MATCHER.addURI(ContentURI.BASE,
            ContentURI.ContentBrowsingHistory.CONTENT_URI_BY_ID.getPath().replaceFirst("/", "") + "/#",
            MATCH_BROWSINGHISTORY_BY_ID);
        
        // 栏目数据操作
        {
            BaseContentProvider category = new CategoryContentProviderImpl();
            map.put(MATCH_CATEGORY_ALL, category);
            map.put(MATCH_CATEGORY_BY_ID, category);
            
            MATCHER.addURI(ContentURI.BASE,
                ContentURI.CategoryList.CONTENT_URI_ALL.getPath().replaceFirst("/", ""),
                MATCH_CATEGORY_ALL);
            
            MATCHER.addURI(ContentURI.BASE, ContentURI.CategoryList.CONTENT_URI_BY_ID.getPath().replaceFirst("/", "")
                + "/#", MATCH_CATEGORY_BY_ID);
        }
        
        // 产品数据操作
        {
            BaseContentProvider Product = new ProductContentProviderImpl();
            map.put(MATCH_Product_ALL, Product);
            map.put(MATCH_Product_BY_ID, Product);
            
            MATCHER.addURI(ContentURI.BASE,
                ContentURI.ProductList.CONTENT_URI_ALL.getPath().replaceFirst("/", ""),
                MATCH_Product_ALL);
            
            MATCHER.addURI(ContentURI.BASE, ContentURI.ProductList.CONTENT_URI_BY_ID.getPath().replaceFirst("/", "")
                + "/#", MATCH_Product_BY_ID);
        }
        // 一键过滤数据操作
        {
        	
        	BaseContentProvider onekeyFilterProvider = new OnekeyFilterProviderImpl();
        	map.put(MATCH_OnekeyFilter_ALL, onekeyFilterProvider);
        	map.put(MATCH_OnekeyFilter_BY_ID, onekeyFilterProvider);
        	
        	MATCHER.addURI(ContentURI.BASE,
        			ContentURI.OnekeyFilterList.CONTENT_URI_ALL.getPath().replaceFirst("/", ""),
        			MATCH_OnekeyFilter_ALL);
        	
        	MATCHER.addURI(ContentURI.BASE, ContentURI.OnekeyFilterList.CONTENT_URI_BY_ID.getPath().replaceFirst("/", "")
        			+ "/#", MATCH_OnekeyFilter_BY_ID);
        	
        }
        
     // 首页模块数据
        {
        	
        	BaseContentProvider homeDataContentProvider = new HomeDataContentProviderImpl();
        	map.put(MATCH_Home_Module_Data_BY_ID, homeDataContentProvider);
        	map.put(MATCH_Home_Module_Data_ALL, homeDataContentProvider);
        	
        	MATCHER.addURI(ContentURI.BASE,
        			ContentURI.HomeModuleData.CONTENT_URI_ALL.getPath().replaceFirst("/", ""),
        			MATCH_Home_Module_Data_ALL);
        	
        	MATCHER.addURI(ContentURI.BASE, ContentURI.HomeModuleData.CONTENT_URI_BY_ID.getPath().replaceFirst("/", "")
        			+ "/#", MATCH_Home_Module_Data_BY_ID);
        	
        }
        
        // 热搜模块数据
        {
        	
        	HotWordProviderImpl hotWordContentProvider = new HotWordProviderImpl();
        	map.put(MATCH_HOT_WORD_BY_ID, hotWordContentProvider);
        	map.put(MATCH_HOT_WORD_ALL, hotWordContentProvider);
        	
        	MATCHER.addURI(ContentURI.BASE,
        			ContentURI.HotWord.CONTENT_URI_ALL.getPath().replaceFirst("/", ""),
        			MATCH_HOT_WORD_ALL);
        	
        	MATCHER.addURI(ContentURI.BASE, ContentURI.HotWord.CONTENT_URI_BY_ID.getPath().replaceFirst("/", "")
        			+ "/#", MATCH_HOT_WORD_BY_ID);
        	
        }
        
    }
    
    /**
     * 获取指定的uri是否是根据 id 进行操作
     * 
     * @param uri
     * @return [参数说明]
     * @return boolean [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static boolean isOperateById(Uri uri)
    {
        int mapping = MATCHER.match(uri);
        if (mapping <= MATCHER_ROOT)
        {
            throw new RuntimeException("unknown uri, " + uri);
        }
        return mapping % 2 == 0;
    }
    
    /**
     * 获取内容提供者实例对象
     * 
     * @param uri
     * @return [参数说明]
     * @return BaseContentProvider [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static BaseContentProvider getContentProvier(Uri uri)
    {
        int mapping = MATCHER.match(uri);
        if (mapping <= MATCHER_ROOT)
        {
            throw new RuntimeException("unknown uri, " + uri);
        }
        return map.get(mapping);
    }
    
}
